﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simulacion.NumberGenerator;

namespace Simulacion.Distributions
{
    /// <summary>
    ///  Generic class of the Frechet distribution.
    /// </summary>
    class Frechet
    {
        public static double fFrechet(double x, double alpha, double beta, double gamma)
        {
            return (alpha / beta) * Math.Pow(beta / (x - gamma), alpha + 1.0) * Math.Exp(-1.0 * Math.Pow(beta / (x - gamma), alpha));
        }

        /// <summary>
        /// Gets the value using the Frechet distribution.
        /// </summary>
        /// <returns>The value of the distribution.</returns>
        public static double GetDistributionValue(double lowerBound, double upperBound, double alpha, double beta, double gamma)
        {
            double _totalArea, _boundedArea, _lowerBoundArea;
            double R = Generator.NextNumber;

            _totalArea = 1.0;
            _boundedArea = Math.Exp(-1.0 * Math.Pow(beta / (upperBound - gamma), alpha)) - Math.Exp(-1.0 * Math.Pow(beta / (lowerBound - gamma), alpha));
            _lowerBoundArea = Math.Exp(-1.0 * Math.Pow(beta / (lowerBound - gamma), alpha));

            R = (_lowerBoundArea / _totalArea) + R * (_boundedArea / _totalArea);

            return (beta / Math.Pow(-1.0 * Math.Log(R), 1.0 / alpha) + gamma) / 7;
        }
    }
}
